﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MapExportTools : EditorWindow
{
    static string writePath = "/Scripts/Maps/";
    static GameObject selectObj = null;

    private Texture2D texture2D;

    [MenuItem("AStar/ExportMapData")]
    public static void run() {
        EditorWindow.GetWindow<MapExportTools>();
    }

    private void OnGUI()
    {
        GUILayout.Label("导出A*地图数据");
        writePath = GUILayout.TextField(writePath);
        GUILayout.Label("请选择TileCreator的节点");

        if (GUILayout.Button("生成C#地图数据"))
        {
            if (selectObj != null)
            {
                AStarMapUitl.CreatCSharpMapFile(selectObj, writePath);
            }
        }
        if (GUILayout.Button("生成C/C++地图数据"))
        {
            if (selectObj != null)
            {
                // CreatMapUitl.CreatCSharpMapFile(selectObj, writePath);
            }
        }

        if (GUILayout.Button("生成Java地图数据"))
        {
            if (selectObj != null)
            {
                // CreatMapUitl.CreatCSharpMapFile(selectObj, writePath);
            }
        }
        if (GUILayout.Button("生成JS地图数据"))
        {
            if (selectObj != null)
            {
                // CreatMapUitl.CreatJSMapFile(selectObj, writePath);
            }
        }

        if (GUILayout.Button("生成Ts地图数据"))
        {
            if (selectObj != null)
            {
                AStarMapUitl.CreatTSMapFile(selectObj, writePath);
            }
        }

        if (GUILayout.Button("生成二进制地图数据"))
        {
            if (selectObj != null)
            {
                // CreatMapUitl.CreatCSharpMapFile(selectObj, writePath);
            }
        }

        if (Selection.activeGameObject != null)
        {
            if (Selection.activeGameObject.GetComponent<TileCreator>())
            {
                GUILayout.Label(Selection.activeGameObject.name);
                selectObj = Selection.activeGameObject;
            }
        }

        GUITexture();
    }

    private void OnSelectionChange()
    {
        Repaint();
    }

    private void GUITexture()
    {
        var startX = 3;
        var startY = 200;
        var textureSize = 256;
        texture2D = (Texture2D)EditorGUI.ObjectField(new Rect(startX, startY, textureSize, textureSize), "select Texture", texture2D, typeof(Texture2D), false);
        if (GUI.Button(new Rect(startX+textureSize+5, startY, 100, 20), "导入"))
        {
            if (texture2D)
            {
                Debug.LogError($"width : {texture2D.width}, height : {texture2D.height}");
                var pixels = texture2D.GetPixels();
                Debug.LogError(pixels.Length);
            }
        }
    }
}
